/**
 * 服务商代码
 * 
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-09-26 18:33:32
 *</pre>
 */
$(function() {
	doorController  = new DoorController();
	doorController.init();
	
	formUrl = doorController.formUrl;
});

(function() {
	//定义常量
	var 	_consts = {
			GRID : "#doorControllerGrid",// 列表对象
			PAGER : "#doorControllerPager",// 列表分页
			FORM : '#doorControllerForm',// 表单form
			FORMGET : '#doorControllerFormGet'// 表单form
			
	};
	/**
	 * 服务商代码 对象
	 * @returns {DoorController}
	 */
	DoorController = function() {
		//定义属性
	};

	/**
	 * 方法
	 */
	DoorController.prototype = {
		consts:	_consts,
		/**
		 * 初始化
		 */
		init : function() {
			if (this.hasInit) // 是否已初始化
				return false;		
			this.hasInit = true;
			if ($(this.consts.GRID).length > 0){//列表
				this._initGridList();
				
			}
			if ($(this.consts.FORM).length > 0){//表单
				this._initForm();
				this._initData();
				this._initOffice('e');
			}
			if ($(this.consts.FORMGET).length > 0){// 明细页面office控件初始化
				this._initOffice('r');
			}
		},
		_initOffice : function(_rights){
		},

		/**
		 * 初始列表
		 */
		_initGridList : function() {
			var me = this;
			$(this.consts.GRID).GridList(
					{
						url :  __ctx+'/rdp/zoneCTL/doorController/listJson.htm',
						pager :this.consts.PAGER,
						colNames: ['CONTROLLERID','产品序列号','控制器名称','启用','IP地址','端口','子网掩码','网关','所在区域ID','所在区域','备注','所控制的门','管理'],
				        colModel: [{
				                 	   name:'controllerid',
				                	   index: 'CONTROLLERID'

										
				                	 	,hidden:true,key:true
				                	 	}, {
				                 	   name:'controllersn',
				                	   index: 'CONTROLLERSN'

										
				                	 					                	 	}, 
			                	 	 {
				                 	   name:'controllerno',
				                	   index: 'CONTROLLERNO'

										
				                	 					                	 	},{
				                 	   name:'enabled',
				                	   index: 'ENABLED',
				                	   formatter:function(cellValue){
				                		   if(cellValue=='1'){
				                			   return "是" ;
				                		   }else{
				                			   return "否" ;
				                		   }	   
				                	   }

										
				                	 					                	 	}, {
				                 	   name:'ipa',
				                	   index: 'IPA'

										
				                	 					                	 	},{
				                 	   name:'port',
				                	   index: 'PORT'

										
										},
										{
						                 	   name:'submask',
						                	   index: 'SUBMAK'

												
						                	 					                	 	}, 
			                	 	{
				                 	   name:'gateway',
				                	   index: 'GATEWAY'

										
				                	 					                	 	},   	 					                	 	
									  {
				                 	   name:'zoneid',
				                	   index: 'ZONEID',
				                	   hidden:true

										
																													},
																													{
				                 	   name:'zonename',
				                	   index: 'ZONENAME'


										
																													},
																													{
				                 	   name:'note',
				                	   index: 'NOTE'

										
				                	 					                	 	}, {
				                 	   name:'doorsnames',
				                	   index: 'DOORSNAMES'

										
				                	 					                	 	}, {
									name : '__manage',
									width : 30,
									sortable:false,
									classes:'rowOps',
									formatter : 'manage',
									formatoptions :[{
										label:'编辑',
										classes:'btn btn-primary fa fa-edit',
										action:__ctx+'/rdp/zoneCTL/doorController/edit.htm?controllerid={id}'
									},{
										label:'删除',
										classes:'btn btn-primary fa fa-remove',
										action:__ctx+'/rdp/zoneCTL/doorController/remove.htm?controllerid={id}'
									}
									//,{
									//	label:'明细',
									//	classes:'btn btn-primary fa fa-detail',
									//	action: __ctx+'/rdp/zoneCTL/doorController/get.htm?controllerid={id}'
									//}
				                  ]
								} ]
	
					});
		},
		
		/**
		 * 初始化表单
		 */
		_initForm : function() {
			var me = this, form = $(this.consts.FORM), frm = form.form();
			me.formUrl = new com.lc.form.FormData(form);
			// 触发表单验证
			frm.valid();
			// 处理表单保存
			$(document).on('click', 'a.fa-save', function() {
				var $el = $(this);
				//alert("执行保存");
				
				var controllerid=$("#controllerid").val(); 
				 if(controllerid!=null){
					//先保存网络参数再保存本地
						var r = sendParam();
						if(!r){
							DialogUtil.confirm("修改网络设备参数失败");
		                    return;
						}
				 }
				
			
				$el.button('loading');	
				 var sn = $("#ctlno").val(); 
		 
				 if (sn.startsWith("1")) {
					  var doorid1 = $("#doorsid11").val() !=  undefined ? $("#doorsid11").val() : "";
					  var doorno1 = sn+"_1"//生成门编号
					  var name1 = $("#doorsnames11").val();
					  var pc1 = $("input[name='ctrl11']:checked").val();
					  var pd1 = $("#delay11").val();				
					  var p11 = $("#doors111").prop('checked')? '1':'0';
					  var p12 =$("#doors112").prop('checked')? '1':'0';
					  var pe1 =p11+p12;
					  var z11=$("#zoneid11").val();  //所在区域的数值		
					  //拼接门控制描述描述
					  $("#doorsnamesx").val(doorid1+","+name1+","+doorno1+","+pc1+","+pd1+","+pe1+","+z11);
					  
				  }
			
				  if ($("#ctlno").val().startsWith("2")){
					  var doorid1 = $("#doorsid21").val()!= undefined ?$("#doorsid21").val():"";
					  var doorno1 = sn+"_1"//生成门编号
					  var name1 = $("#doorsnames21").val();
					  var pc1 = $("input[name='ctrl21']:checked").val();
					  var pd1 = $("#delay21").val();				
					  var p11 = $("#doors211").prop('checked')? '1':'0';
					  var p12 =$("#doors212").prop('checked')? '1':'0';
					  var pe1 =p11 + p12;
					  var z21=$("#zoneid21").val();  //所在区域的数值
					 
					  var doorid2 = $("#doorsid22").val()!= undefined ? $("#doorsid22").val():"";
					  var doorno2 = sn+"_2"//生成门编号
					  var name2 = $("#doorsnames22").val();
					  var pc2 = $("input[name='ctrl22']:checked").val();
					  var pd2 = $("#delay22").val();				
					  var p21 = $("#doors221").prop('checked')? '1':'0';
					  var p22 =$("#doors222").prop('checked')? '1':'0';
					  var pe2 =p21 + p22;
					  var z22=$("#zoneid22").val();  //所在区域的数值			  				                  
					  //拼接门控制描述描述

					  $("#doorsnamesx").val(doorid1+","+name1+","+doorno1+","+pc1+","+pd1+","+pe1+","+z21+";"
							              + doorid2+","+name2+","+doorno2+","+pc2+","+pd2+","+pe2+","+z22
					   );
				  } 
				  
			     if ($("#ctlno").val().startsWith("4")){
			    	  var doorid1 = $("#doorsid41").val() != undefined ?$("#doorsid41").val():"";
					  var doorno1 = sn+"_1"//生成门编号
					  var name1 = $("#doorsnames41").val();
					  var pc1 = $("input[name='ctrl41']:checked").val();
					  var pd1 = $("#delay41").val();				
					  var p11 = $("#doors411").prop('checked')? '1':'0';
					  var p12 ='0';
					  var pe1 =p11+p12;
					  var z41=$("#zoneid41").val();  //所在区域的数值
					 // alert("z41="+z41);
					  
					  var doorid2 = $("#doorsid42").val() != undefined ? $("#doorsid42").val():"";
					  var doorno2 = sn+"_2"//生成门编号
					  var name2 = $("#doorsnames42").val();
					  var pc2 = $("input[name='ctrl42']:checked").val();
					  var pd2 = $("#delay42").val();				
					  var p21 = $("#doors421").prop('checked')? '1':'0';
					  var p22 ='0';
					  var pe2 =p21+p22;
					  var z42=$("#zoneid42").val();;  //所在区域的数值
					 // alert("z42="+z42);
					  
					  
					  var doorid3 = $("#doorsid43").val()!= undefined ? $("#doorsid43").val():"";
					  var doorno3 = sn+"_3"//生成门编号
					  var name3 = $("#doorsnames43").val();
					  var pc3 = $("input[name='ctrl43']:checked").val();
					  var pd3 = $("#delay43").val();				
					  var p31 = $("#doors431").prop('checked')? '1':'0';
					  var p32 ='0';
					  var pe3 =p31+p32;
					  var z43=$("#zoneid43").val();; //所在区域的数值
					 // alert("z43="+z43);
					  
					  var doorid4 = $("#doorsid44").val()!= undefined? $("#doorsid44").val():"";
					  var doorno4 = sn+"_4"//生成门编号
					  var name4 = $("#doorsnames44").val();
					  var pc4 = $("input[name='ctrl44']:checked").val();
					  var pd4 = $("#delay44").val();				
					  var p41 = $("#doors441").prop('checked')? '1':'0';
					  var p42 ='0';
					  var pe4 =p41+p42;				  				  				
					  var z44=$("#zoneid44").val();; //所在区域的数值
					 // alert("z44="+z44);
					  //
					  //拼接门控制描述描述
					  $("#doorsnamesx").val(doorid1+","+name1+","+doorno1+","+pc1+","+pd1+","+pe1+","+z41+";"
							              + doorid2+","+name2+","+doorno2+","+pc2+","+pd2+","+pe2+","+z42+";"
							              + doorid3+","+name3+","+doorno3+","+pc3+","+pd3+","+pe3+","+z43+";"
							              + doorid4+","+name4+","+doorno4+","+pc4+","+pd4+","+pe4+","+z44
							              );
				  } 

		  // office提交
    	    OfficePlugin.submit();
			me.formUrl.submit(me._showResponse, $el);
			});
		},
		/**
		 * 初始化数据
		 */
		_initData : function(){
			if(!$.isEmpty(frameElement) 
				&& !$.isEmpty(frameElement.dialog) 
				&& !$.isEmpty(frameElement.dialog.params)
				&& !$.isEmpty(frameElement.dialog.params.data)){
				var data = frameElement.dialog.params.data;
				this.formUrl.setData("[name^='m:']", data);
				AttachementControl.initDataTo($("div[name='div_attachment_container']")); //附件初始化
			}else{
				$("[name^='m:']", $(_consts.FORM)).each(function(){
					var data = $(this);
	        		// 单选框特殊处理 
	            	if(data.is('input') && "radio" == data.attr("type")){
			        		var defaultVal = data.attr("defaultVal");
			        		var defaultValue = data.attr("defaultValue");
		            		if(defaultValue=="true"&&defaultVal=="true"){
		            			data.prop("checked", "checked");
		            		}
		        	}
		        });
			}
			
			this.formUrl.validate();
		},
		/**
		 * 表单成功返回信息
		 * 
		 * @param responseText
		 */
		_showResponse : function(responseText) {
			var msg = new com.lc.form.ResultMessage(responseText);
			if (msg.isSuccess()) {
				DialogUtil.confirm(msg.getMessage() + ',是否继续操作',
					function(rtn) {
					if(rtn)
						window.location.reload(true);
					else
						window.location.href = __ctx+'/rdp/zoneCTL/doorController/list.htm';
					});
			} else {
				DialogUtil.error(msg.getMessage());
			}
		}
	};
})();